﻿Imports System.Data.SqlClient
Public Class frmMesParraines

    'Matériels
    'Fonctions et procédures
    Public Sub remplirLstTous()
        lstViwClt.Items.Clear()
        Cmd = New SqlCommand("select * from Clients " & _
                                "where Id_Client in( " & _
                                "select idClt from paraiClt " &
                                "where idParai = " & idParrai & ")", Con)
        Dr = Cmd.ExecuteReader
        If Dr.HasRows Then
            While Dr.Read
                Dim Line As ListViewItem = New ListViewItem(New String() {Dr.GetInt32(0), Dr.GetString(1), Dr.GetString(2), Dr.GetString(4), Dr.GetString(3)})
                lstViwClt.Items.Add(Line)
            End While
        End If
        Dr.Close()
        cmbClt.SelectedIndex = -1
        txtPrix.Clear()
    End Sub

    Private Sub frmLiaiParinClt_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            remplirCmb(cmbParrin, "select idParai, nom + ' ' + prenom from Paraineur")
            remplirCmb(cmbClt, "select Id_Client, nom + ' ' + prenom from Clients")
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        Dim prix As Double = 0
        Dim isPrixEmpty As Boolean = False
        Dim prixVrai As Boolean = False
        Try
            If cmbParrin.SelectedIndex >= 0 And cmbClt.SelectedIndex >= 0 Then
                'Rassuré le prix
                If Not String.IsNullOrWhiteSpace(txtPrix.Text) And Not String.IsNullOrEmpty(txtPrix.Text) Then
                    Double.TryParse(txtPrix.Text, prix)
                Else
                    isPrixEmpty = True
                End If

                If Not isPrixEmpty Then
                    If prix >= 0 Then
                        prixVrai = True
                    Else
                        MsgBox("Prix incorrecte!!!" & Environment.NewLine & "Vous avez entrez beaucoup de virgules", MsgBoxStyle.Critical, "Erreur")
                        Exit Sub
                    End If
                End If
                'End

                If isPrixEmpty Or prixVrai Then
                    Cmd = New SqlCommand("INSERT INTO [paraiClt]([idClt],[idParai])VALUES(" & _
                 CType(cmbClt.SelectedItem, DictionaryEntry).Key & "," & CType(cmbParrin.SelectedItem, DictionaryEntry).Key & ")", Con)
                    open()
                    Cmd.ExecuteNonQuery()


                    Cmd.CommandText = "SELECT @@IDENTITY AS 'Identity'"
                    Dim idParrainage As Integer = Cmd.ExecuteScalar()
                    Cmd.CommandText = "INSERT INTO [SommeParain]([idParain],[somme]) VALUES(" & idParrainage & ",@prix)"
                    Cmd.Parameters.Add("@prix", SqlDbType.Float)
                    Cmd.Parameters("@prix").Value = Math.Round(prix, 2)
                    Cmd.ExecuteNonQuery()

                    remplirLstTous()
                    cnClose()
                    txtPrix.Clear()
                    cmbClt.SelectedIndex = -1
                    MsgBox("Le client est bien ajouté à ma liste", MsgBoxStyle.Information, "succès")
                End If
            Else
                MsgBox("Vous devez séléctioner un parraineur et un client", MsgBoxStyle.Critical, "Erreur")
            End If
        Catch sqlEx As SqlException
            If sqlEx.Message.Contains("uniqueCltParrai") Then
                MsgBox("Ce client est déjà dans ma liste", MsgBoxStyle.Critical, "Erreur")
            Else
                MsgBox(sqlEx.Message, MsgBoxStyle.Critical, "Erreur")
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub cmbParrin_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbParrin.SelectedIndexChanged
        'If Not selectedChangedCmb Then
        Try
            idParrai = CType(cmbParrin.SelectedItem, DictionaryEntry).Key
            open()
            remplirLstTous()
            cnClose()
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
        'End If
        'selectedChangedCmb = False
    End Sub

    Private Sub lstViwClt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstViwClt.SelectedIndexChanged
        Try
            If isLstSlctdChged Then
                If lstViwClt.SelectedItems.Count > 0 Then
                    idClt = CInt(lstViwClt.SelectedItems(0).SubItems(0).Text)
                    For Each it As DictionaryEntry In cmbClt.Items
                        If it.Key = idClt Then
                            isSelectedChanged = False
                            cmbClt.SelectedItem = it
                            Exit For
                        End If
                    Next


                    Cmd = New SqlCommand("select somme from SommeParain " & _
                                        "where idParain in ( " & _
                                        "select paraiClt.idParainage from paraiClt " & _
                                        "where idClt = " & CType(cmbClt.SelectedItem, DictionaryEntry).Key & _
                                        "and idParai = " & CType(cmbParrin.SelectedItem, DictionaryEntry).Key & ")", Con)
                    open()
                    txtPrix.Text = Math.Round(Cmd.ExecuteScalar, 2)
                    cnClose()
                Else
                    txtPrix.Clear()
                    cmbClt.SelectedIndex = -1
                End If
            End If
            isSelectedChanged = True
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub btnSupp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupp.Click
        Try
            If lstViwClt.SelectedItems.Count > 0 Then
                Cmd = New SqlCommand("DELETE FROM [paraiClt] WHERE idClt = " & idClt, Con)
                open()
                Cmd.ExecuteNonQuery()
                cnClose()
                For Each lstIt As ListViewItem In lstViwClt.Items
                    If CInt(lstIt.SubItems(0).Text) = idClt Then
                        lstIt.Remove()
                        Exit For
                    End If
                Next
                MsgBox("Le client est bien supprimé de ma liste", MsgBoxStyle.Information, "succès")
                txtPrix.Clear()
                cmbClt.SelectedIndex = -1
            Else
                MsgBox("Veuillez sélectionner un client de la list", MsgBoxStyle.Critical, "Erreur")
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Dim isLstSlctdChged As Boolean = True 'Savoir s'il faut déclancher l'événement selected changed pour la list
    Private Sub cmbClt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbClt.SelectedIndexChanged
        Try
            If isSelectedChanged Then
                For Each it As ListViewItem In lstViwClt.SelectedItems
                    isLstSlctdChged = False
                    it.Selected = False
                    txtPrix.Clear()
                Next
            End If
            isLstSlctdChged = True
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub txtPrix_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrix.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub btnRechParrai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechParrai.Click
        Me.Enabled = False
        frmRechParrai.Show()
    End Sub

    Private Sub btnRechClt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechClt.Click
        Me.Enabled = False
        frmRechClt.Show()
    End Sub

    Private Sub btnPayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPayer.Click
        Try
            If lstViwClt.SelectedItems.Count > 0 Then
                If txtPrix.Text <> "0" And Not String.IsNullOrEmpty(txtPrix.Text) And Not String.IsNullOrWhiteSpace(txtPrix.Text) Then
                    Cmd = New SqlCommand("select * from SommeParain " &
                                            "where idParain in " &
                                            "(select idParainage from paraiClt where idClt = " & idClt & " and idParai= " & idParrai & ") ", Con)
                    open()
                    Dr = Cmd.ExecuteReader
                    If Dr.HasRows Then
                        Dr.Read()
                        somParrain = New sommeParain
                        With somParrain
                            .idParain = Dr.GetInt32(0)
                            .somme = Dr.GetDouble(1)
                        End With
                        Dr.Close()
                        cnClose()
                        Me.Enabled = False
                        frmPaiementParai.Show()
                    Else
                        MsgBox("Ce parrainage est gratuit!!!" & Environment.NewLine & "Si ce n'est pas le cas veuillez entrer un prix pour ce dernier", MsgBoxStyle.Exclamation, "Erreur")
                    End If


                Else
                    MsgBox("Ce parrainage est gratuit!!!", MsgBoxStyle.Critical, "Erreur")
                End If
            Else
                MsgBox("Veuillez sélectionner un client de la list", MsgBoxStyle.Critical, "Erreur")
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub
End Class